package com.crm.controller.ordersController;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.OpportunityDAO;
import com.crm.dao.POConfirmDAO;
import com.crm.dao.SupplierDAO;
import com.crm.entity.Business;
import com.crm.entity.Opportunity;
import com.crm.entity.POConfirm;
import com.crm.entity.Supplier;


@Controller
@RequestMapping("/poConfirm")
public class POConfirmationController {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping( "/show.do") 
	public ModelAndView poConfirmation(HttpServletRequest request,HttpSession session, ModelAndView model) throws Exception{
		
		Business bus=(Business)session.getAttribute("business");
		
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		
			//OpportunityDAO oprD=new OpportunityDAO();
			List<Opportunity> orders=(List<Opportunity>)OpportunityDAO.getSingleInstance().findAll(busid);
					logger.info("opportunity list is "+orders);
			  model = new ModelAndView("/jsp/cdec.jsp");
			  model.addObject("submenu", "orders");
				model.addObject("menuName","orders");
				model.addObject("orders",orders);
			
				return model;
	}
	@RequestMapping( "/save.do") 
	public ModelAndView save(HttpServletRequest request,HttpSession session, ModelAndView model) throws Exception{
		System.out.println("Starting Of Save In PO Confirm");
		Business bus=(Business)session.getAttribute("business");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		
		String refno=request.getParameter("enqid");
		//OpportunityDAO oprD=new OpportunityDAO();
		Opportunity o=OpportunityDAO.getSingleInstance().getByRefNo(busid,refno);
		int enqid=o.getId();
		String supplierName=request.getParameter("supplier");
		
		byte[] cdecReqLetter=null;
		
			MultipartFile cdecfile = multipartRequest.getFile("cdecfile");
			try {
				  cdecReqLetter=cdecfile.getBytes();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
		String materialStatus=request.getParameter("materialStatus");
		String couriername=null;
		String trackingNo=null;
		if(materialStatus!=null){
			couriername=request.getParameter("courierName");
			trackingNo=request.getParameter("trackingNumber");
		}
		
		SupplierDAO supDAO=new SupplierDAO();
		Supplier s=supDAO.getByName(supplierName,bus.getBusinessId());
		
		int supid=s.getId();
		
		
		byte[] supplierProforma=null;
		
		MultipartFile supplierproforma = multipartRequest.getFile("supplierProforma");
		/*logger.info("File:" + file.getName());*/
		logger.info("ContentType:" + supplierproforma.getContentType());
		try {
			  supplierProforma=supplierproforma.getBytes();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		int poid=0;
		POConfirm poconfirm=new POConfirm(enqid, poid, supid,supplierProforma, cdecReqLetter, couriername, trackingNo);
		POConfirmDAO pocdao=new POConfirmDAO();
		try{
			pocdao.insert(poconfirm);
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println("After Inserting The PO COnfirm");
		
		model = new ModelAndView("redirect:show.do");
		model.addObject("msg","Successfully Saved The PO Confirmation");
		return model;
	}
}
